package org.zls.server;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.zls.client.Course;
import org.zls.client.service.ServerService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ServerServiceImpl extends RemoteServiceServlet implements
		ServerService {

	private static final long serialVersionUID = 709315055972594682L;

	public long getPublicKey() {
		return 122108073;
	}
	
	public long demystify(long msg) {
		System.out.println("Received Msg: " + msg);
		long salt = msg % 10000;
		msg = msg / 10000;
		
		System.out.println("RetrievedSalt: " + salt);
		System.out.println("Message to decode: " + msg);
		
		long key = getPublicKey() * 10000 + salt;
		
		return msg ^ key;
	}
	
	public boolean verifyCredentials(long e, long p) {
		long eep = demystify(e);
		long pee = demystify(p);
		
		System.out.println("Demystified e: " + eep);
		System.out.println("Demystified p: " + pee);
		
		return true;
	}
	
	// TODO: Optimize search with indices
	// TODO: Consider limiting search results (paging)
	// TODO: We should consider how we tag courses such that Organismic Biology is tagged with Biology and Organismic
	public String[][] getTableData(String query, String[] cols) {
		Db db;
		try {
			int length = 0;
			db = new Db("localhost", "mysql", "root", "root", "hspot");
			ArrayList<String[]> results = new ArrayList<String[]>();
			String msg = "SELECT courses.* FROM course_attrs, courses WHERE course_attrs.attr_id = (SELECT attr_id FROM attrs WHERE attr LIKE '" + query + "') AND course_attrs.course_id = courses.course_id";
			ResultSet rs = db.executeQuery(msg);

			while (rs.next()) {
				length++;
				String[] result = new String[cols.length];
				for (int i = 0; i < cols.length; i++) {
					result[i] = rs.getString(cols[i]);
				}
				results.add(result);
			}

			rs.close();
			
			String[][] data = new String[length][cols.length];
			
			for (int i = 0; i < length; i++) {
				for (int j = 0; j < cols.length; j++) {
					data[i][j] = results.get(i)[j];
				}
			}

			return data;

		} catch (SQLException e) {
			System.out.println("ERROR: Failed to connect to server in getTableData().");
			e.printStackTrace();
		}
		return new String[][] {new String[] {"Hello"}};
	}

	public ArrayList<String> populateSearchBox() {
		Db db;
		try {
			db = new Db("localhost", "mysql", "root", "root", "hspot");
			ArrayList<String> results = new ArrayList<String>();
			ResultSet rs = db
					.executeQuery("SELECT attr FROM attrs WHERE searchbox_attrs=TRUE;");

			while (rs.next())
				results.add(rs.getString("attr"));

			rs.close();

			return results;
		} catch (SQLException e) {
			System.out.println("ERROR: Failed to connect to server in populateSearchBox().");
			e.printStackTrace();
		}
		return null;
	}
	
	public Course getCourseData(int course_id) {
		Db db;
		Course course = new Course();
		try {
//			int length = 0;
			db = new Db("localhost", "mysql", "root", "root", "hspot");
//			ArrayList<String[]> results = new ArrayList<String[]>();
			String msg = "SELECT * FROM courses WHERE course_id = " + course_id;
//			String msg = "SELECT * FROM courses WHERE course_id = '" + course_id + "'";
//			String msg = "SELECT courses.* FROM course_attrs, courses WHERE course_attrs.attr_id = (SELECT attr_id FROM attrs WHERE attr LIKE '" + query + "') AND course_attrs.course_id = courses.course_id";
			ResultSet rs = db.executeQuery(msg);

			System.out.println(rs.getStatement());
			
			rs.next();
			
			course.setCourse_id(course_id);
			course.setCatalog(rs.getInt("catalog"));
			course.setSize(rs.getInt("size"));
			course.setDepartment(rs.getString("department"));
			course.setDescription(rs.getString("description"));
			course.setMeetings(rs.getString("meetings"));
			course.setName(rs.getString("name"));
			course.setPrimarily(rs.getString("primarily"));
			course.setProfessor(rs.getString("professor"));
			course.setQ_difficulty(rs.getString("q_difficulty"));
			course.setQ_overall(rs.getString("q_overall"));
			course.setQ_professor(rs.getString("q_professor"));
			course.setQ_readings(rs.getString("q_readings"));
			course.setQ_workload(rs.getString("q_workload"));
			course.setTerm(rs.getString("term"));
			course.setTitle(rs.getString("title"));
			
			rs.close();
			
		} catch (SQLException e) {
			System.out.println("ERROR: Failed to connect to server in getCourseData().");
			e.printStackTrace();
		}
		return course;
	}

}
